import { get, post } from '@/utils/request'
import md5 from 'md5';

// 生成签名
function generateSign(agent, timeStamp, md5Key) {
    // 模拟MD5签名生成过程
    // 实际签名规则: 移除首尾各2位，第9和17位后各移除2位
    const rawSign = md5(agent + timeStamp + md5Key).toString();

    // 按照PlatRequestFilter中的规则处理签名
    let processedSign = "xx" + rawSign.substring(0, 9) + "xx" +
        rawSign.substring(9, 17) + "xx" +
        rawSign.substring(17) + "xx";
    return processedSign;
}

// 发送请求的通用方法
async function requstParams(method, data) {
    const agent = "test_agent";
    const timeStamp = Date.now();
    const randno = Math.floor(Math.random() * 1000000);
    const md5Key = "test_md5_key@";
    const sign = generateSign(agent, timeStamp, md5Key);

    const params = new URLSearchParams();
    params.append('agent', agent);
    params.append('timeStamp', timeStamp);
    params.append('randno', randno);
    params.append('sign', sign);

    let url = '';
    if (method === 'GET' && data) {
        Object.keys(data).forEach(key => {
            if (data[key] !== undefined && data[key] !== '') {
                params.append(key, data[key]);
            }
        });
        url += params.toString();
    } else {
        url += params.toString();
    }

    return url
}

// 4.1 获取游戏配置 - GET /plat/gameConfig
export const getGameConfig = (opts = {}) => {
  return get('/plat/gameConfig?' + requstParams('GET', opts))
}

// 4.2 启动游戏 - POST /plat/launchGame
// platUser: { account, pwd, game, memberIp? }
export const launchGame = (platUser, opts = {}) => {
  const { agent = DEFAULT_AGENT, secretKey = DEFAULT_SECRET_KEY } = opts
  return post('/plat/launchGame', { platUser }, { params: buildCommonParams(agent, secretKey) })
}

// 4.3 上分 - POST /plat/transferIn
// transferInfo: { account, pwd, money, orderId }
export const transferIn = (transferInfo, opts = {}) => {
  const { agent = DEFAULT_AGENT, secretKey = DEFAULT_SECRET_KEY } = opts
  return post('/plat/transferIn', { transferInfo }, { params: buildCommonParams(agent, secretKey) })
}

// 4.4 下分 - POST /plat/transferOut
// transferInfo: { account, pwd, money, orderId }
export const transferOut = (transferInfo, opts = {}) => {
  const { agent = DEFAULT_AGENT, secretKey = DEFAULT_SECRET_KEY } = opts
  return post('/plat/transferOut', { transferInfo }, { params: buildCommonParams(agent, secretKey) })
}

// 4.5 查询余额 - POST /plat/remain
// platUser: { account, pwd }
export const remain = (platUser, opts = {}) => {
  const { agent = DEFAULT_AGENT, secretKey = DEFAULT_SECRET_KEY } = opts
  return post('/plat/remain', { platUser }, { params: buildCommonParams(agent, secretKey) })
}

// 4.6 转账记录查询 - POST /plat/transferLog
// logInfo: { account, pwd, orderId?, startTime?, endTime?, pageNum?, pageSize? }
export const transferLog = (logInfo, opts = {}) => {
  const { agent = DEFAULT_AGENT, secretKey = DEFAULT_SECRET_KEY } = opts
  return post('/plat/transferLog', { logInfo }, { params: buildCommonParams(agent, secretKey) })
}

export default {
  getGameConfig,
  launchGame,
  transferIn,
  transferOut,
  remain,
  transferLog,
}